<?php


namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\course\Course;
use app\admin\model\course\CourseContent;
use app\admin\model\course\Train;
use app\admin\model\course\UserCourse;
use app\common\controller\Api;

class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //订单预览
    public function orderpreview()
    {
        $coursecontent_id = $this->request->request('coursecontent_id');//课程内容ID
        if(!$coursecontent_id){
            $this->error(__('Invalid parameters'));
        }

        //课程内容信息
        $coursecontent = CourseContent::get($coursecontent_id);//课程内容
        if(!$coursecontent){
            $this->error(__('信息错误'));
        }
        $course = Course::get($coursecontent->course_id);//课程信息
        $train = Train::get($course->train_id);//培训机构

        $this->success('返回成功', [
            'coursecontent' => $coursecontent,
            'course' => $course,
            'train' => $train
        ]);
    }

    //订单详情
    public function orderdetails()
    {
        $user = $this->auth->getUser();
        $order_id = $this->request->request('order_id');//订单ID
        if(!$order_id){
            $this->error(__('Invalid parameters'));
        }
        $order = \app\admin\model\Order::get($order_id);//订单内容
        if(!$order){
            $this->error(__('信息错误'));
        }
        $coursecontent = CourseContent::get($order->coursecontent_id);//课程内容信息
        $course = Course::get($coursecontent->course_id);//课程信息
        $train = Train::get($course->train_id);//培训机构

        $this->success('返回成功', [
            'order' => $order,
            'coursecontent' => $coursecontent,
            'course' => $course,
            'train' => $train
        ]);
    }

    //订单提交
    public function ordersubmit()
    {
        $user = $this->auth->getUser();
        $coursecontent_id = $this->request->request('coursecontent_id');//课程内容ID
        if(!$coursecontent_id){
            $this->error(__('Invalid parameters'));
        }
        $coursecontent = CourseContent::get($coursecontent_id);//课程内容

        //检测用户报名课程重复
        $ret = \app\admin\model\Order::get(['coursecontent_id' => $coursecontent_id, 'user_id' => $user->id]);
        if($ret){
            $this->error(__('当前课程已报名'));
        }

        $data = [
            'order_no' => generate_order_no(),
            'user_id' =>  $user->id,
            'coursecontent_id' => $coursecontent_id,
            'image' => $coursecontent->image,
            'price' => $coursecontent->price,
            'is_pay' => 0
        ];

        $res =  \app\admin\model\Order::create($data);
        if($res) {
            $this->success('支付成功',$res);
        }else{
            $this->error(__('支付失败'));
        }
    }

    public function payorder()
    {
        $user = $this->auth->getUser();
        $coursecontent_id = $this->request->request('coursecontent_id');//课程内容ID
        if(!$coursecontent_id){
            $this->error(__('Invalid parameters'));
        }
        $params = [

        ];
        //        Service::submitOrder();
    }

    //订单列表
    public function orderlist()
    {
        $limit = $this->request->request('limit') ? $this->request->request('limit') : 10;//长度
        $page = $this->request->request('page');//当前页数

        $user = $this->auth->getUser();
        $order_model = new \app\admin\model\Order();
        $query = $order_model
            ->where(['user_id' => $user->id]);
        $row = $query
            ->with(['coursecontent'])
            ->limit($limit,$page)
            ->order("createtime desc")
            ->select();

        $total = $query->count();
        $row_count = ceil($total / $limit) ;

        $this->success('返回成功',[
            "total" => $total,
            "row" => $row,
            "row_count" => $row_count
        ]);
    }
}